package com.iwhalecloud.bss.kite.cucc.common.callclient;

import java.util.HashMap;
import java.util.Map;

import com.iwhalecloud.bss.kite.cart.common.util.KiteSessionUtil;
import org.apache.commons.collections.MapUtils;

import com.alibaba.fastjson.JSON;
import com.iwhalecloud.bss.kite.common.callclient.constant.ICallClientEnum;
import com.iwhalecloud.bss.kite.common.callclient.service.IOrderQueryService;
import com.iwhalecloud.bss.kite.common.constant.ErrorConsts;
import com.iwhalecloud.bss.kite.common.entity.InfService;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.callclient.service.ICuccQueryInfService;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalCallClientEnum;
import com.iwhalecloud.bss.kite.cucc.common.util.KiteHttpsUtils;
import com.iwhalecloud.bss.kite.cucc.common.util.attackUtil.KiteAttackSignKey;

import com.ztesoft.bss.common.util.SpringUtil;

/**
 * <Description> http服务调用 <br>
 *
 * @author huang.caibiao<br>
 * @version 1.0<br>
 * @taskId <br>
 * @date 2020-02-25 <br>
 * @since R9.0<br>
 * @see com.iwhalecloud.bss.kite.common.callclient <br>
 */
public abstract class HttpsCallClient {


    public static Map post(ICallClientEnum callClientEnum, Map<String, String> busiMap) {
        return post(callClientEnum.getCenterCode(), callClientEnum.getServiceCode(), busiMap, null);
    }

    public static Map post(ICallClientEnum callClientEnum, Map<String, String> busiMap, Map<String, String> headerMap) {
        return post(callClientEnum.getCenterCode(), callClientEnum.getServiceCode(), busiMap, headerMap);
    }

    public static Map post(String centerCode, String serviceCode, Map<String, String> busiMap) {
        return post(centerCode, serviceCode, busiMap, null);
    }

    public static Map post(String centerCode, String serviceCode, Map<String, String> busiMap, Map<String, String> headerMap) {
        // 获取服务配置信息
        IOrderQueryService orderQueryService = SpringUtil.getBean(IOrderQueryService.class);
        InfService infService = orderQueryService.queryServiceBySvcCode(serviceCode, centerCode);

        if (infService == null) {
            ErrorConsts.SERVICE_UNREGISTERED.throwOut(centerCode + ":" + serviceCode);
        }

        if (headerMap == null) {
            headerMap = new HashMap<String, String>();
        }
        headerMap.put("Cookie", "SESSION=" + KiteSessionUtil.getLocalSessionId());

        // 针对post的请求,然后过对端系统的AntiAttackFilter校验
        // 只对inf_service.param_types=api/v2的接口进行这样的处理
        if (KiteStringUtils.isNotEmpty(infService.getParamTypes()) && infService.getParamTypes().contains("api/v2")) {
            headerMap.put("signKey", new KiteAttackSignKey().getSignKey(busiMap));
        }

        String rsp = KiteHttpsUtils.doPost(null, SpringUtil.getBean(ICuccQueryInfService.class).getUrlByType(infService), headerMap, busiMap, new HashMap<>());

        return JSON.parseObject(rsp, Map.class);

        /*Map result = JSON.parseObject(rsp, Map.class);
        if (infService.getClassName().indexOf("/goods/goodsInfo") >= 0) {
            return KiteMapUtils.getMap(result, "data");
        }
        if ((result.containsKey("success") && !"true".equals(KiteMapUtils.getString(result, "success")))
                || (result.containsKey("code") && !"200".equals(KiteMapUtils.getString(result, "code")))) {
            ErrorConsts.INF_INVOKE_ERROR.throwOut(infService.getSvcCode(), infService.getSvcDesc(), KiteMapUtils.getString(result, "message"));
        }
        return KiteMapUtils.getMap(result, "data");*/


    }


    public static Map get(ICallClientEnum callClientEnum, Map<String, String> busiMap) {
        return get(callClientEnum.getCenterCode(), callClientEnum.getServiceCode(), busiMap, null);
    }

    public static Map get(ICallClientEnum callClientEnum, Map<String, String> busiMap, Map<String, String> headerMap) {
        return get(callClientEnum.getCenterCode(), callClientEnum.getServiceCode(), busiMap, headerMap);
    }

    public static Map get(String centerCode, String serviceCode, Map<String, String> busiMap) {
        return get(centerCode, serviceCode, busiMap, null);
    }

    public static Map get(String centerCode, String serviceCode, Map<String, String> busiMap, Map<String, String> headerMap) {
        // 获取服务配置信息
        IOrderQueryService orderQueryService = SpringUtil.getBean(IOrderQueryService.class);
        InfService infService = orderQueryService.queryServiceBySvcCode(serviceCode, centerCode);

        if (infService == null) {
            ErrorConsts.SERVICE_UNREGISTERED.throwOut(centerCode + ":" + serviceCode);
        }

        String rsp = KiteHttpsUtils.doGet(null, SpringUtil.getBean(ICuccQueryInfService.class).getUrlByType(infService), headerMap, busiMap);

        return JSON.parseObject(rsp, Map.class);
    }


    public static String postString(ICallClientEnum callClientEnum, Map<String, String> busiMap) {
        return postString(callClientEnum.getCenterCode(), callClientEnum.getServiceCode(), busiMap, null);
    }

    public static String postString(ICallClientEnum callClientEnum, Map<String, String> busiMap, Map<String, String> headerMap) {
        return postString(callClientEnum.getCenterCode(), callClientEnum.getServiceCode(), busiMap, headerMap);
    }

    public static String postStringWithObject(ICallClientEnum callClientEnum, Map<String, Object> busiMap, Map<String, String> headerMap) {
        return postStringWithObject(callClientEnum.getCenterCode(), callClientEnum.getServiceCode(), busiMap, headerMap);
    }

    public static String getString(ICallClientEnum callClientEnum, Map<String, String> busiMap) {
        return getString(callClientEnum.getCenterCode(), callClientEnum.getServiceCode(), busiMap, null);
    }

    public static String getString(ICallClientEnum callClientEnum, Map<String, String> busiMap, Map<String, String> headerMap) {
        return getString(callClientEnum.getCenterCode(), callClientEnum.getServiceCode(), busiMap, headerMap);
    }

    private static String getString(String centerCode, String serviceCode, Map<String, String> busiMap, Map<String, String> headerMap) {
        // 获取服务配置信息
        IOrderQueryService orderQueryService = SpringUtil.getBean(IOrderQueryService.class);
        InfService infService = orderQueryService.queryServiceBySvcCode(serviceCode, centerCode);

        if (infService == null) {
            ErrorConsts.SERVICE_UNREGISTERED.throwOut(centerCode + ":" + serviceCode);
        }
        // 查询登录信息不做灰度处理
        if (KiteStringUtils.isEqual(centerCode, LocalCallClientEnum.QUERY_LOGININFO.getCenterCode()) && KiteStringUtils
            .isEqual(serviceCode, LocalCallClientEnum.QUERY_LOGININFO.getServiceCode())) {
            return KiteHttpsUtils.doGetNonGray(null, SpringUtil.getBean(ICuccQueryInfService.class).getUrlByType(infService), headerMap, busiMap);
        }
        return KiteHttpsUtils.doGet(null, SpringUtil.getBean(ICuccQueryInfService.class).getUrlByType(infService), headerMap, busiMap);

        /*Map<String, String> result = JSON.parseObject(rsp, Map.class);
        if(KiteMapUtils.getString(result, "code").equals("200") || KiteMapUtils.getString(result, "code").equals("0")) {
            return KiteMapUtils.getString(result, "data");
        }
        if (!"true".equals(KiteMapUtils.getString(result, "success"))) {
            ErrorConsts.INF_INVOKE_ERROR.throwOut(infService.getSvcCode(), infService.getSvcDesc(), KiteMapUtils.getString(result, "message"));
        }
        return KiteMapUtils.getString(result, "data");*/
    }

    public static String postString(String centerCode, String serviceCode, Map<String, String> busiMap, Map<String, String> headerMap) {
        // 获取服务配置信息
        IOrderQueryService orderQueryService = SpringUtil.getBean(IOrderQueryService.class);
        InfService infService = orderQueryService.queryServiceBySvcCode(serviceCode, centerCode);

        if (infService == null) {
            ErrorConsts.SERVICE_UNREGISTERED.throwOut(centerCode + ":" + serviceCode);
        }

        if (headerMap == null) {
            headerMap = new HashMap<String, String>();
        }
        headerMap.put("Cookie", "SESSION=" + KiteSessionUtil.getLocalSessionId());
        return KiteHttpsUtils.doPost(null, SpringUtil.getBean(ICuccQueryInfService.class).getUrlByType(infService), headerMap, busiMap, new HashMap<>());

        /*if (KiteStringUtils.isEmpty(rsp)) {
            return "";
        }
        Map<String, String> result = JSON.parseObject(rsp, Map.class);
        if("SUCCESS".equals(KiteMapUtils.getString(result, "message")) || "0".equals(KiteMapUtils.getString(result, "status"))) {
            return KiteMapUtils.getString(result, "data");
        }
        if (!"true".equals(KiteMapUtils.getString(result, "success"))) {
            ErrorConsts.INF_INVOKE_ERROR.throwOut(infService.getSvcCode(), infService.getSvcDesc(), KiteMapUtils.getString(result, "message"));
        }
        return KiteMapUtils.getString(result, "data");*/


    }

    public static String postStringWithObject(String centerCode, String serviceCode, Map<String, Object> busiMap, Map<String, String> headerMap) {
        // 获取服务配置信息
        IOrderQueryService orderQueryService = SpringUtil.getBean(IOrderQueryService.class);
        ICuccQueryInfService cuccQueryInfService = SpringUtil.getBean(ICuccQueryInfService.class);
        InfService infService = orderQueryService.queryServiceBySvcCode(serviceCode, centerCode);

        if (infService == null) {
            ErrorConsts.SERVICE_UNREGISTERED.throwOut(centerCode + ":" + serviceCode);
        }

        if (headerMap == null) {
            headerMap = new HashMap<String, String>();
        }
        headerMap.put("Cookie", "SESSION=" + KiteSessionUtil.getLocalSessionId());
        return KiteHttpsUtils.doPostWithObject(null, cuccQueryInfService.getUrlByType(infService), headerMap, busiMap, new HashMap<>());
    }

    public static String postStringWithObjectWithoutLog(String centerCode, String serviceCode, Map<String, Object> busiMap, Map<String, String> headerMap) {
        // 获取服务配置信息
        IOrderQueryService orderQueryService = SpringUtil.getBean(IOrderQueryService.class);
        ICuccQueryInfService cuccQueryInfService = SpringUtil.getBean(ICuccQueryInfService.class);
        InfService infService = orderQueryService.queryServiceBySvcCode(serviceCode, centerCode);

        if (infService == null) {
            ErrorConsts.SERVICE_UNREGISTERED.throwOut(centerCode + ":" + serviceCode);
        }

        if (headerMap == null) {
            headerMap = new HashMap<String, String>();
        }
        headerMap.put("Cookie", "SESSION=" + KiteSessionUtil.getLocalSessionId());
        return KiteHttpsUtils.doPostWithObjectWithoutLog(null, cuccQueryInfService.getUrlByType(infService), headerMap, busiMap, new HashMap<>());
    }

    /**
     * 负载对象入参调用
     * @param callClientEnum
     * @param busiMap
     * @return
     */
    public static Map postObjMap(ICallClientEnum callClientEnum, Map<String, Object> busiMap) {
        return postObjMap(callClientEnum.getCenterCode(), callClientEnum.getServiceCode(), busiMap, null);
    }

    public static Map postObjMap(String centerCode, String serviceCode, Map<String, Object> busiMap, Map<String, String> headerMap) {
        // 获取服务配置信息
        IOrderQueryService orderQueryService = SpringUtil.getBean(IOrderQueryService.class);
        InfService infService = orderQueryService.queryServiceBySvcCode(serviceCode, centerCode);

        if (infService == null) {
            ErrorConsts.SERVICE_UNREGISTERED.throwOut(centerCode + ":" + serviceCode);
        }

        if (headerMap == null) {
            headerMap = new HashMap<String, String>();
        }
        headerMap.put("Cookie", "SESSION=" + KiteSessionUtil.getLocalSessionId());

        String rsp = KiteHttpsUtils.doPostObjMap(null, SpringUtil.getBean(ICuccQueryInfService.class).getUrlByType(infService), headerMap, busiMap, headerMap);

        if (KiteStringUtils.isEmpty(rsp)) {
            return MapUtils.EMPTY_MAP;
        }

        return JSON.parseObject(rsp, Map.class);
    }
}
